﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QldtSdh.BLL;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.Utils;

namespace QldtSdh.GUI.DuLieu
{
    public partial class FormQuanLyHocPhan : QldtSdh.GUI.Common.BaseDialog
    {
        bool allowUpdate = false;
        public FormQuanLyHocPhan()
        {
            InitializeComponent();
            gridData.AddRowIndexing();
            InitGrid();
            allowUpdate = false;
            if (LoadDonViPhuTrach())
                LoadGrid();
            allowUpdate = true;
        }

        bool LoadDonViPhuTrach()
        {
            var ls = BizDonViDaoTao.GetList();

            cmbDonViPhuTrach.DataSource = ls;
            cmbDonViPhuTrach.DisplayMember = "TenDonVi";
            cmbDonViPhuTrach.ValueMember = "MaDonVi";
            if (ls == null || ls.Count == 0)
            {
                btnThem.Enabled = false;
                return false;
            }
            else
            {
                btnThem.Enabled = true;
                return true;
            }
        }

        void InitGrid()
        {
            gridData.AutoGenerateColumns = false;
            colMaHocPhan.DataPropertyName = "MaHocPhan";
            colTenHocPhan.DataPropertyName = "TenHocPhan";
            colSoTinChi.DataPropertyName = "SoTinChi";
        }

        void LoadGrid()
        {
            if (cmbDonViPhuTrach.SelectedIndex != -1)
            {
                DonVi donViPhuTrach = cmbDonViPhuTrach.SelectedItem as DonVi;
                bdData.DataSource = BizHocPhan.GetList(donViPhuTrach.MaDonViGUID);
                gridData.DataSource = bdData;
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            var frm = new DuLieu.FormQuanLyHocPhan_ChiTiet(cmbDonViPhuTrach.SelectedItem as DonVi);
            frm.OnChanged += (o, t) =>
            {
                LoadGrid();
            };
            frm.ShowDialog();
        }

        private void cmbDonViPhuTrach_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (allowUpdate)
            {
                LoadGrid();
            }
        }

        private void bdData_CurrentChanged(object sender, EventArgs e)
        {
            var current = bdData.Current as HocPhan;
            if (current != null)
            {
                btnSua.Enabled = btnXoa.Enabled = true;
                HocPhan_PropertyGrid hocPhanProperty = new HocPhan_PropertyGrid()
                {
                    MaHocPhan = current.MaHocPhan,
                    TenHocPhan = current.TenHocPhan,
                    HinhThucHoc = current.HinhThucHoc,
                    HinhThucThi = current.HinhThucThi,
                    DonViPhuTrach = BizDonViDaoTao.GetByID(current.MaDonViPhuTrachGUID).TenDonVi,
                    DiemQTHT = current.HeSoDiemQTHT.ToString(),
                    DiemThi = (100 - current.HeSoDiemQTHT).ToString(),
                    SoTinChi = current.SoTinChi.ToString(),
                    TongSoGio = current.TongSoGio.ToString(),
                    GhiChu = current.GhiChu
                };
                propertyGridHocPhan.SelectedObject = hocPhanProperty;
            }
            else
            {
                btnSua.Enabled = btnXoa.Enabled = false;
                propertyGridHocPhan.SelectedObject = null;
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            var current = bdData.Current as HocPhan;
            if (current != null)
            {
                var frm = new DuLieu.FormQuanLyHocPhan_ChiTiet(cmbDonViPhuTrach.SelectedItem as DonVi, current);
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    LoadGrid();
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            var current = bdData.Current as HocPhan;
            if (current != null)
            {
                if (DialogUtils.ShowOKCancel(NOTIFY_CONSTANT_STRING.DELETE) == System.Windows.Forms.DialogResult.OK)
                {
                    var rs = BizHocPhan.Delete(current.MaHocPhanGUID);
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                        bdData.RemoveCurrent();
                    else
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_XOA, ENTITY_NAME.HOCPHAN));
                }
            }
        }

        private void lnkNapLaiDanhSachDonVi_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            LoadDonViPhuTrach();
        }
    }

    [DefaultPropertyAttribute("MaHocPhan")]
    public class HocPhan_PropertyGrid
    {
        private const string A = "A. Thông tin chung";
        [CategoryAttribute(A), DisplayName("1. Mã học phần")]
        public string MaHocPhan { get; set; }
        [CategoryAttribute(A), DisplayName("2. Tên học phần")]
        public string TenHocPhan { get; set; }
        [CategoryAttribute(A), DisplayName("3. Đơn vị phụ trách")]
        public string DonViPhuTrach { get; set; }
        [CategoryAttribute(A), DisplayName("4. Số tín chỉ")]
        public string SoTinChi { get; set; }
        [CategoryAttribute(A), DisplayName("5. Tổng số giờ")]
        public string TongSoGio { get; set; }
        [CategoryAttribute(A), DisplayName("6. Ghi chú")]
        public string GhiChu { get; set; }

        private const string B = "B. Hình thức";
        [CategoryAttribute(B), DisplayName("1. Hình thức học")]
        public string HinhThucHoc { get; set; }
        [CategoryAttribute(B), DisplayName("2. Hình thức thi")]
        public string HinhThucThi { get; set; }

        private const string C = "C. Hệ số tính điểm";
        [CategoryAttribute(C), DisplayName("1. Điểm QTHT")]
        public string DiemQTHT { get; set; }
        [CategoryAttribute(C), DisplayName("2. Điểm thi")]
        public string DiemThi { get; set; }
    }
}
